clear, clc, close all

% This code replicates Figures 1 to 5 in
% Ayres, Navarro, Nicolini and Teles: "Self-Fulfilling Debt Crises with Long
% Stagnations"

%% Figure 1: Expected return function for different levels of debt (different b's)

% Parameter values
yl = 3;             % low endowment (yl)
yh = 6;             % high endowment (yh)
yd = 1;             % endowment under default (yd)
p = 0.4;            % probability of low endowment
kappa = 0.2;        % fraction of debt recovered
Rs = 1.2;           % risk-free rate
b1 = 0.9;           % low debt level
b2 = 1.4;           % medium debt level
b3 = 2.2;           % high debt level

% Figure specification
xlim1 = 0.5;        % lower bound in x-axis
xlim2 = 6.0;        % upper bound in x-axis
ylim1 = 0.0;        % lower bound in y-axis
ylim2 = 3.5;        % upper bound in y-axis
fontsz = 10;        % font size

% Figure 1(a) - low debt (b)
figure
plot([xlim1 (yl-yd)/b1+kappa],[xlim1 (yl-yd)/b1+kappa],'Color',[0.8 0 0],'LineWidth',2), hold on
plot([(yl-yd)/b1+kappa (yh-yd)/b1+kappa],[(1-p)*((yl-yd)/b1+kappa) (1-p)*((yh-yd)/b1+kappa)],'Color',[0.8 0 0],'LineWidth',2)
plot([(yh-yd)/b1+kappa xlim2],[kappa kappa],'Color',[0.8 0 0],'LineWidth',2)
plot([(yl-yd)/b1+kappa (yl-yd)/b1+kappa],[(yl-yd)/b1+kappa (1-p)*((yl-yd)/b1+kappa)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yh-yd)/b1+kappa (yh-yd)/b1+kappa],[(1-p)*((yh-yd)/b1+kappa) kappa],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([xlim1 xlim2],[Rs Rs],'Color','k','LineWidth',1.0)
plot([Rs Rs],[0 Rs],'Color','k','LineWidth',1.0,'LineStyle',':')
set(gca,'XGrid','on','YGrid','on','Fontsize',fontsz);
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
xlabel('$R$','Interpreter','LaTex','Fontsize',fontsz)
ylabel('$h(R;b)$','Interpreter','LaTex','Fontsize',fontsz)
set(gca,'XTickLabel',[],'YTickLabel',[])
text(0.6,1.35,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color','k')
text(Rs-0.1,-0.2,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz*.75,'Color','k')
text(3.8,3.1,'low $b$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
set(gca,'Fontsize',fontsz,'FontName','Helvetica')
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [4.5 1.7]; % [width height]
paper_position = [0 0 4.5 1.7]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_exp_return_low_b.pdf';
%saveas(gcf,filename)

% Figure 1(b) - medium debt (b)
figure
plot([xlim1 (yl-yd)/b2+kappa],[xlim1 (yl-yd)/b2+kappa],'Color',[0.8 0 0],'LineWidth',2), hold on
plot([(yl-yd)/b2+kappa (yh-yd)/b2+kappa],[(1-p)*((yl-yd)/b2+kappa) (1-p)*((yh-yd)/b2+kappa)],'Color',[0.8 0 0],'LineWidth',2)
plot([(yh-yd)/b2+kappa xlim2],[kappa kappa],'Color',[0.8 0 0],'LineWidth',3.25)
plot([(yl-yd)/b2+kappa (yl-yd)/b2+kappa],[(yl-yd)/b2+kappa (1-p)*((yl-yd)/b2+kappa)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yh-yd)/b2+kappa (yh-yd)/b2+kappa],[(1-p)*((yh-yd)/b2+kappa) kappa],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([xlim1 xlim2],[Rs Rs],'Color','k','LineWidth',1.0)
plot([Rs Rs],[0 Rs],'Color','k','LineWidth',1.0,'LineStyle',':')
plot([Rs/(1-p) Rs/(1-p)],[0 Rs],'Color','k','LineWidth',1.0,'LineStyle',':')
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
xlabel('$R$','Interpreter','LaTex','Fontsize',fontsz)
ylabel('$h(R;b)$','Interpreter','LaTex','Fontsize',fontsz)
set(gca,'XTickLabel',[],'YTickLabel',[])
text(0.6,1.35,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color','k')
text(Rs-0.1,-0.2,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz*0.75,'Color','k')
text(Rs/(1-p)-0.2,-0.30,'$\frac{R^{\ast}-p\kappa}{1-p}$','Interpreter','LaTex','Fontsize',fontsz*0.85,'Color','k')
text(3.5,2.7,'medium $b$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
set(gca,'Fontsize',fontsz,'FontName','Helvetica')
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [4.5 1.7]; % [width height]
paper_position = [0 0 4.5 1.7]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_exp_return_medium_b.pdf';
%saveas(gcf,filename)

% Figure 1(c) - high debt (b)
figure
plot([xlim1 (yl-yd)/b3+kappa],[xlim1 (yl-yd)/b3+kappa],'Color',[0.8 0 0],'LineWidth',2), hold on
plot([(yl-yd)/b3+kappa (yh-yd)/b3+kappa],[(1-p)*((yl-yd)/b3+kappa) (1-p)*((yh-yd)/b3+kappa)],'Color',[0.8 0 0],'LineWidth',2)
plot([(yh-yd)/b3+kappa xlim2],[kappa kappa],'Color',[0.8 0 0],'LineWidth',3.25)
plot([(yl-yd)/b3+kappa (yl-yd)/b3+kappa],[(yl-yd)/b3+kappa (1-p)*((yl-yd)/b3+kappa)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yh-yd)/b3+kappa (yh-yd)/b3+kappa],[(1-p)*((yh-yd)/b3+kappa) kappa],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([xlim1 xlim2],[Rs Rs],'Color','k','LineWidth',1.0)
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
xlabel('$R$','Interpreter','LaTex','Fontsize',fontsz)
ylabel('$h(R;b)$','Interpreter','LaTex','Fontsize',fontsz)
plot([Rs/(1-p) Rs/(1-p)],[0 Rs],'Color','k','LineWidth',1.0,'LineStyle',':')
set(gca,'XTickLabel',[],'YTickLabel',[])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
text(0.6,1.35,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color','k')
text(Rs/(1-p)-0.2,-0.30,'$\frac{R^{\ast}-p\kappa}{1-p}$','Interpreter','LaTex','Fontsize',fontsz*0.85,'Color','k')
text(2.0,1.8,'high $b$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
set(gca,'Fontsize',fontsz,'FontName','Helvetica')
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [4.5 1.7]; % [width height]
paper_position = [0 0 4.5 1.7]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_exp_return_high_b.pdf';
%saveas(gcf,filename)

%% Figure 2: Interest rate schedules (low and high interest rate schedules)

% Parameter values
yl = 3.6;           % low endowment (yl)
yh = 5.6;           % high endowment (yh)
yd = 1;             % endowment under default (yd)
p = 0.3;            % probability of low endowment
kappa = 0.2;        % fraction of debt recovered
Rs = 1.2;           % risk-free rate

% Figure specification
xlim1 = 1.2;        % lower bound in x-axis
xlim2 = 3.7;        % upper bound in x-axis
ylim1 = 0.9;        % lower bound in y-axis
ylim2 = 2.0;        % upper bound in y-axis
fontsz = 11;        % font size
fontsz2 = 10;       % font size (alternative)

% Figure 2(a): low interest rate schedule
figure
plot([0 (yl-yd)/(Rs-kappa)],[Rs Rs],'Color',[0.8 0 0],'LineWidth',2.5,'LineStyle','--'), hold on
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineWidth',2.5,'LineStyle','--')
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yl-yd)/(Rs-kappa)],[0 (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([0 (1-p)*(yl-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yl-yd)/(Rs-kappa) (yl-yd)/(Rs-kappa)],[0 Rs],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(1-p)*(yh-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[0 (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([0 (yl-yd)/(Rs-kappa)],[Rs Rs],'Color',[0 0 0.8],'LineWidth',2.0)
plot([(yl-yd)/(Rs-kappa) (yl-yd)/(Rs-kappa)],[Rs (Rs-p*kappa)/(1-p)],'Color',[0 0 0.8],'LineWidth',1.0,'LineStyle',':')
plot([(yl-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0 0 0.8],'LineWidth',2.0)
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
set(gca,'XTick',[],'YTick',[])
set(gca,'XTickLabel',[],'YTickLabel',[])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
text(1.07,1.2,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text(0.99,(Rs-p*kappa)/(1-p),'$\frac{R^{\ast}-p\kappa}{1-p}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text((1-p)*(yl-yd)/(Rs-kappa)-0.3,ylim1-0.085,'$b_{1} = (1-p)\frac{y^{l}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((yl-yd)/(Rs-kappa)-0.20,ylim1-0.085,'$b_{2} = \frac{y^{l}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((1-p)*(yh-yd)/(Rs-kappa)-0.14,ylim1-0.085,'$\bar{b} = (1-p)\frac{y^{h}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((yl-yd)/(Rs-kappa),0.95*mean([(Rs-p*kappa)/(1-p),ylim2]),'$R^{low}(b)$','Interpreter','LaTex','FontSize',fontsz,'Color',[0 0 0.8])
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.0 2.2]; % [width height]
paper_position = [0 0 6.0 2.2]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_schedule_low.pdf';
%saveas(gcf,filename)

% Figure 2(b): high interest rate schedule
figure
plot([0 (yl-yd)/(Rs-kappa)],[Rs Rs],'Color',[0.8 0 0],'LineWidth',2.5,'LineStyle','--'), hold on
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineWidth',2.5,'LineStyle','--')
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yl-yd)/(Rs-kappa)],[0 (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([0 (1-p)*(yl-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yl-yd)/(Rs-kappa) (yl-yd)/(Rs-kappa)],[0 Rs],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(1-p)*(yh-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[0 (Rs-p*kappa)/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([0 (1-p)*(yl-yd)/(Rs-kappa)],[Rs Rs],'Color',[0 0 0.8],'LineWidth',2.0)
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yl-yd)/(Rs-kappa)],[Rs (Rs-p*kappa)/(1-p)],'Color',[0 0 0.8],'LineWidth',1.0,'LineStyle',':')
plot([(1-p)*(yl-yd)/(Rs-kappa) (1-p)*(yh-yd)/(Rs-kappa)],[(Rs-p*kappa)/(1-p) (Rs-p*kappa)/(1-p)],'Color',[0 0 0.8],'LineWidth',2.0)
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
set(gca,'XTick',[],'YTick',[])
set(gca,'XTickLabel',[],'YTickLabel',[])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
text(1.07,1.2,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text(0.99,(Rs-p*kappa)/(1-p),'$\frac{R^{\ast}-p\kappa}{1-p}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text((1-p)*(yl-yd)/(Rs-kappa)-0.3,ylim1-0.085,'$b_{1} = (1-p)\frac{y^{l}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((yl-yd)/(Rs-kappa)-0.20,ylim1-0.085,'$b_{2} = \frac{y^{l}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((1-p)*(yh-yd)/(Rs-kappa)-0.14,ylim1-0.085,'$\bar{b} = (1-p)\frac{y^{h}-y^{d}}{R^{\ast}-\kappa}$','Interpreter','LaTex','Fontsize',fontsz2,'Color',[0 0 0])
text((yl-yd)/(Rs-kappa),0.95*mean([(Rs-p*kappa)/(1-p),ylim2]),'$R^{high}(b)$','Interpreter','LaTex','FontSize',fontsz,'Color',[0 0 0.8])
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.0 2.2]; % [width height]
paper_position = [0 0 6.0 2.2]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_schedule_high.pdf';
%saveas(gcf,filename)

%% Figure 3: Debt policy function

% Parameter values
yl = 11.5;                          % low endowment (yl)
yh = 19.5;                          % high endowment (yh)
yd = 6.5;                           % endowment under default (yd)
p = 0.55;                           % probability of low endowment
kappa = 0.2;                        % fraction of debt recovered
Rs = 1.5;                           % risk-free rate
gamma = 5.5;                        % risk aversion parameter (CRRA preference)
beta= 0.2;                          % discount factor
Nw = 200;                           % size of grid for initial wealth
wmin = 1.0;                         % minimum value for wealth in the grid
wmax = 11.7;                        % maximum value for wealth in the grid
w_grid = linspace(wmin,wmax,Nw);    % grid for initial wealth
pol_fcn_low   = w_grid*0;           % policy function for debt under the low interest rate schedule
pol_fcn_high  = w_grid*0;           % policy function for debt under the high interest rate schedule
b1   = (1-p)*(yl-yd)/(Rs-kappa);    % debt threshold above which the high interest rate induces default
b2   = (yl-yd)/(Rs-kappa);          % debt threshould under which the low interest rate does not induce default
bbar = (1-p)*(yh-yd)/(Rs-kappa);    % debt limit
r_high = (Rs-p*kappa)/(1-p);        % high interest rate
f_U = @(x) (x^(1-gamma))/(1-gamma); % utility function
f_ObjFcn = @(b,w,r) (-1)*(f_U(w+b)+beta*(p*f_U(max(yd-kappa*b,yl-r*b))+(1-p)*f_U(max(yd-kappa*b,yh-r*b)))); % objective function
eps = 1.0e-7;                       % precision parameter
u_low = -1.0e+6;                    % lower bound in utility
cand_b = zeros(2,1);                % candidates for optimum borrowing choice: one per interest rate segment.
cand_u = zeros(2,1);                % utility from picking the candidate for optimum borrowing choice: one per interest rate segment.
% Low interest rate schedule 
for i = 1:Nw
    wi = w_grid(i);         % wealth
    % First candidate (optimal borrowing in the low interest rate segment)
    [b_opt1,u_opt1] = fminbnd(@(x) f_ObjFcn(x,wi,Rs),-wi+eps,b2-eps);
    cand_b(1) = b_opt1;
    cand_u(1) = -u_opt1;
    % Second candidate (optimal borrowing in the high interest rate segment)
    [b_opt2,u_opt2] = fminbnd(@(x) f_ObjFcn(x,wi,r_high),b2+eps,bbar-eps); 
    cand_b(2) = b_opt2;
    cand_u(2) = -u_opt2;
    % Policy function
    [aux1,loc] = max(cand_u);
    pol_fcn_low(i) = cand_b(loc);
end
cand_b = cand_b*0;                % candidates for optimum borrowing choice: one per interest rate segment.
cand_u = cand_u*0;                % utility from picking the candidate for optimum borrowing choice: one per interest rate segment.
% High interest rate schedule 
for i = 1:Nw
    wi = w_grid(i);         % wealth
    % First candidate (optimal borrowing in the low interest rate segment)
    [b_opt1,u_opt1] = fminbnd(@(x) f_ObjFcn(x,wi,Rs),-wi+eps,b1-eps);
    cand_b(1) = b_opt1;
    cand_u(1) = -u_opt1;
    % Second candidate (optimal borrowing in the high interest rate segment)
    [b_opt2,u_opt2] = fminbnd(@(x) f_ObjFcn(x,wi,r_high),b1+eps,bbar-eps);    
    cand_b(2) = b_opt2;
    cand_u(2) = -u_opt2;
    % Policy function
    [aux1,loc] = max(cand_u);
    pol_fcn_high(i) = cand_b(loc);
end
ind_low1 = (pol_fcn_low>b2);
ind_low2 = (ind_low1==0);
iwlow = sum(ind_low1);
ind_high1 = (pol_fcn_high>b1);
ind_high2 = (ind_high1==0);
iwhigh = sum(ind_high1);
figure
fontsz = 11;
plot(w_grid(ind_low1),pol_fcn_low(ind_low1),'Color',[0 0 0.8],'LineWidth',2.5), hold on
plot(w_grid(ind_low2),pol_fcn_low(ind_low2),'Color',[0 0 0.8],'LineWidth',2.5)
plot([w_grid(iwlow) w_grid(iwlow)],[pol_fcn_low(iwlow) pol_fcn_low(iwlow+1)],'Color',[0 0 0.8],'LineWidth',1.0,'LineStyle',':')
plot(w_grid(ind_high1),pol_fcn_high(ind_high1),'Color',[0.8 0 0],'LineWidth',2.0,'LineStyle','--')
plot(w_grid(ind_high2),pol_fcn_high(ind_high2),'Color',[0.8 0 0],'LineWidth',2.0,'LineStyle','--')
plot([w_grid(iwhigh) w_grid(iwhigh)],[pol_fcn_high(iwhigh) pol_fcn_high(iwhigh+1)],'Color',[0.8 0 0],'LineWidth',1.0,'LineStyle',':')
xlim([wmin,wmax])
ylim([1.3,4.7])
set(gca,'XTick',[],'YTick',[b1,b2,bbar])
set(gca,'XTickLabel',[],'YTickLabel',[])
text(9,2.7,'$b(\omega;R^{low}(b))$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0.8])
text(5.5,4.0,'$b(\omega;R^{high}(b))$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
text(wmin-0.5,b1,'$b_1$','Interpreter','LaTex','Fontsize',fontsz*1.1,'Color',[0 0 0])
text(wmin-0.5,b2,'$b_2$','Interpreter','LaTex','Fontsize',fontsz*1.1,'Color',[0 0 0])
text(wmin-0.4,bbar,'$\overline{b}$','Interpreter','LaTex','Fontsize',fontsz*1.1,'Color',[0 0 0])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
xlabel('initial wealth ($\omega$)','Interpreter','LaTex','Fontsize',fontsz)
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.5 2.3]; % [width height]
paper_position = [0 0 6.5 2.3]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_polfcn.pdf';
%saveas(gcf,filename)

%% Figure 4: Interest rate correspondence for different p 

% Parameter values
yl = 3;             % low endowment (yl)
yh = 4;             % high endowment (yh)
yd = 1;             % endowment under default (yd)
kappa = 0.2;        % fraction of debt recovered
Rs = 1.2;           % risk-free rate
ph = 0.25;          % high p
pl = 0.15;          % low p

% Figure specification
xlim1 = 0.8;        % lower bound in x-axis
xlim2 = 2.5;        % upper bound in x-axis
ylim1 = 1.0;        % lower bound in y-axis
ylim2 = 1.8;        % upper bound in y-axis
fontsz = 11;        % font size

figure
plot([0 (yl-yd)/Rs],[Rs Rs],'Color',[0 0 0.8],'LineWidth',2.5), hold on
plot([(1-ph)*(yl-yd)/Rs (1-ph)*(yh-yd)/Rs],[Rs/(1-ph) Rs/(1-ph)],'Color',[0 0 0.8],'LineWidth',2.5)
plot([(1-ph)*(yl-yd)/Rs (1-ph)*(yl-yd)/Rs],[0 Rs/(1-ph)],'Color',[0 0 0.8],'LineStyle',':','LineWidth',1.0)
plot([0 (1-ph)*(yl-yd)/Rs],[Rs/(1-ph) Rs/(1-ph)],'Color',[0 0 0.8],'LineStyle',':','LineWidth',1.0)
plot([(yl-yd)/Rs (yl-yd)/Rs],[0 Rs],'Color',[0 0 0.8],'LineStyle',':','LineWidth',1.0)
plot([(1-ph)*(yh-yd)/Rs (1-ph)*(yh-yd)/Rs],[0 Rs/(1-ph)],'Color',[0 0 0.8],'LineStyle',':','LineWidth',1.0)
plot([0 (yl-yd)/Rs],[Rs Rs],'Color',[0.8 0 0],'LineWidth',2.0,'LineStyle','--')
plot([(1-pl)*(yl-yd)/Rs (1-pl)*(yh-yd)/Rs],[Rs/(1-pl) Rs/(1-pl)],'Color',[0.8 0 0],'LineWidth',2.0,'LineStyle','--')
plot([(1-pl)*(yl-yd)/Rs (1-pl)*(yl-yd)/Rs],[0 Rs/(1-pl)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([0 (1-pl)*(yl-yd)/Rs],[Rs/(1-pl) Rs/(1-pl)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yl-yd)/Rs (yl-yd)/Rs],[0 Rs],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(1-pl)*(yh-yd)/Rs (1-pl)*(yh-yd)/Rs],[0 Rs/(1-pl)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
text((1-ph)*(yl-yd)/(Rs-kappa)+0.05,Rs/(1-ph)+0.05,'high p','Color',[0 0 0.8],'FontSize',fontsz)
text((1-pl)*(yl-yd)/(Rs-kappa)-0.1,Rs/(1-pl)+0.05,'low p','Color',[0.8 0 0],'FontSize',fontsz)
xlim([xlim1,xlim2])
ylim([ylim1,ylim2])
set(gca,'XTick',[],'YTick',[])
set(gca,'XTickLabel',[],'YTickLabel',[])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
ylabel('$R$','Interpreter','LaTex','Fontsize',fontsz)
xlabel('debt ($b$)','Interpreter','LaTex','Fontsize',fontsz)
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.5 2.3]; % [width height]
paper_position = [0 0 6.5 2.3]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_schedule_p.pdf';
%saveas(gcf,filename)

%% Figure 5: Varying the standard deviation of endowment shock, σ

% Figure 5(a): expected return function
yl = 4;                          % low endowment (yl)
yh = 6;                          % high endowment (yh)
yd = 1;                          % endowment under default (yd)
p = 0.4;                         % probability of low endowment
kappa = 0.2;                     % fraction of debt recovered
Rs = 1.2;                        % risk-free rate
b = 2.3;                         % debt level
sigma1 = 0.3;                    % medium standard deviation (sigma)
sigma2 = 0.7;                    % high standard deviation (sigma)
xlim1 = 0.9;                     % lower bound, x-axis
xlim2 = 2.5;                     % upper bound, x-axis
ylim1 = 0.8;                     % lower bound, y-axis
ylim2 = 1.6;                     % upper bound, y-axis
fontsz = 11;                     % fontsize
func_plot = @(x,y) x*(1 - p*normcdf(yd+(x-kappa)*b,yl,y) - (1-p)*normcdf(yd+(x-kappa)*b,yh,y)) + kappa*(p*normcdf(yd+(x-kappa)*b,yl,y) + (1-p)*normcdf(yd+(x-kappa)*b,yh,y));
figure
plot([xlim1 (yl-yd)/b+kappa],[xlim1 (yl-yd)/b+kappa],'Color',[0.8 0 0],'LineWidth',2.0), hold on
plot([(yl-yd)/b+kappa (yh-yd)/b+kappa],[(1-p)*((yl-yd)/b+kappa) (1-p)*((yh-yd)/b+kappa)],'Color',[0.8 0 0],'LineWidth',2.0)
plot([(yh-yd)/b+kappa xlim2],[kappa kappa],'Color',[0.8 0 0],'LineWidth',2.0)
plot([(yl-yd)/b+kappa (yl-yd)/b+kappa],[(yl-yd)/b+kappa (1-p)*((yl-yd)/b+kappa)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([(yh-yd)/b+kappa (yh-yd)/b+kappa],[(1-p)*((yh-yd)/b+kappa) kappa],'Color',[0.8 0 0],'LineStyle',':','LineWidth',1.0)
plot([xlim1 xlim2],[Rs Rs],'k','LineWidth',1.0)
pf1 = fplot(@(x) func_plot(x,sigma1),[xlim1 xlim2]);
set(pf1,'LineWidth',2.5,'Color',[0 0 0.8],'LineStyle',':')
pf2 = fplot(@(x) func_plot(x,sigma2),[xlim1 xlim2]);
set(pf2,'LineWidth',2.0,'Color',[0 0 0],'LineStyle','--')
xlim([xlim1 xlim2])
ylim([ylim1 ylim2])
xlabel('$R$','Interpreter','LaTex','Fontsize',fontsz)
ylabel('$h(R;b)$','Interpreter','LaTex','Fontsize',fontsz)
set(gca,'XTick',[],'YTick',[])
set(gca,'XTickLabel',[],'YTickLabel',[])
set(gca,'XGrid','off','YGrid','off','Fontsize',fontsz);
text(1.4,1.55,'$\sigma=0$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
text(1.9,1.37,'$medium \ \sigma$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0.8])
text(2.05,1.0,'$high \ \sigma$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text(xlim1+0.1,Rs+0.02,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color','k')
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.5 2.3]; % [width height]
paper_position = [0 0 6.5 2.3]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_exp_return_sigma.pdf';
%saveas(gcf,filename)

% Figure 5(b): interest rate correspondence
yl = 4;                          % low endowment (yl)
yh = 7;                          % high endowment (yl)
yd = 1;                          % endowment under default (yd)
p = 0.4;                         % probability of low endowment
kappa = 0.2;                     % fraction of debt recovered
Rs = 1.2;                        % risk-free rate
sigma1 = 0.1;                    % medium standard deviation (sigma)
sigma2 = 0.9;                    % high standard deviation (sigma)
b1   = (1-p)*(yl-yd)/(Rs-kappa); % debt threshold above which the high interest rate induces default
b2   = (yl-yd)/(Rs-kappa);       % debt threshould under which the low interest rate does not induce default
bbar = (1-p)*(yh-yd)/(Rs-kappa); % debt limit
xlim1 = 0.4;                     % lower bound, x-axis
xlim2 = 4.0;                     % upper bound, x-axis
ylim1 = 0.8;                     % lower bound, y-axis
ylim2 = 2.7;                     % upper bound, y-axis
fontsz = 11;                     % fontsize
NX = 1000;                       % size of grid for debt
grid_X = linspace(-2,8,NX);      % grid for X = (r-kappa)*b; note that X pins down a unique interest rate r, and then we can recover what the respective b is.
F_bimodal = @(X,sigma) p*normcdf(yd+X,yl,sigma) + (1-p)*normcdf(yd+X,yh,sigma); % default probability as a function of debt issued and standard deviation of endowment distribution
grid_r_sg_medium = zeros(1,NX);  % grid of interest rates under medium standard deviation
grid_r_sg_high   = zeros(1,NX);  % grid of interest rates under high standard deviation
grid_b_sg_medium = zeros(1,NX);  % grid of debt under medium standard deviation
grid_b_sg_high   = zeros(1,NX);  % grid of debt under high standard deviation
for i=1:NX
    grid_r_sg_medium(i) = Rs/(1-F_bimodal(grid_X(i),sigma1));
    grid_b_sg_medium(i) = grid_X(i)/(grid_r_sg_medium(i)-kappa);
    grid_r_sg_high(i)   = Rs/(1-F_bimodal(grid_X(i),sigma2));
    grid_b_sg_high(i)   = grid_X(i)/(grid_r_sg_high(i)-kappa);   
end
figure
plot([0 b2],[Rs Rs],'Color',[0.8 0 0],'LineWidth',2.0), hold on
plot([b1 bbar],[Rs/(1-p) Rs/(1-p)],'Color',[0.8 0 0],'LineWidth',2.0)
plot([0 b1],[Rs/(1-p) Rs/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',0.5)
plot([b1 b1],[xlim1 Rs/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',0.5)
plot([b2 b2],[xlim1 Rs],'Color',[0.8 0 0],'LineStyle',':','LineWidth',0.5)
plot([bbar bbar],[xlim1 Rs/(1-p)],'Color',[0.8 0 0],'LineStyle',':','LineWidth',0.5)
xlim([xlim1,xlim2])
ylim([ylim1,ylim2])
plot(grid_b_sg_medium,grid_r_sg_medium,'LineWidth',2.5,'Color',[0 0 0.8],'LineStyle',':')
plot(grid_b_sg_high,grid_r_sg_high,'LineWidth',1.5,'Color',[0 0 0],'LineStyle','--')
set(gca,'XTick',[],'YTick',[])
set(gca,'XTickLabel',[],'YTickLabel',[])
text(xlim1-0.2,Rs,'$R^{\ast}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text(xlim1-0.35,Rs/(1-p),'$\frac{R^{\ast}-p\kappa}{1-p}$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
xlabel('$b$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
text(b2,Rs+0.1,'$\sigma=0$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0.8 0 0])
text(bbar-0.5,2.4,'medium \ $\sigma$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0.8])
text(b1-0.0,2.4,'high \ $\sigma$','Interpreter','LaTex','Fontsize',fontsz,'Color',[0 0 0])
% specification
paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
paper_size = [6.5 2.3]; % [width height]
paper_position = [0 0 6.5 2.3]; % [left bottom width height].
paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
fig = gcf;
fig.PaperUnits = paper_unit;
fig.PaperSize = paper_size;
fig.PaperPosition = paper_position;
fig.PaperOrientation = paper_orientation;
filename = 'Figures/fig_icorresp_sigma.pdf';
%saveas(gcf,filename)
